<template>
  <el-dialog :append-to-body="true" :before-close="cancelDialog" :close-on-click-modal="false"
             :title="'审计工作底稿'" :visible.sync="visible" custom-class="ledger" fullscreen top="5vh"
             width="1200px">
    <div class="colums">
      <div class="colums_left">
        <div class="colums_left_middle">
          <div class="middleForm ledger" style="overflow-x: hidden">
            <el-form ref="dataForm" :model="form" class="el-dialog__body" label-width="100px">
              <div class="title">项目信息</div>
              <el-row>
                <el-col :span="8">
                  <el-form-item label-width="100px"  label="项目编号:" prop="projectCode">
                    {{ form.projectCode}}
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label-width="100px"  label="项目名称:" prop="projectName">
                    {{ form.projectName }}
                  </el-form-item>
                </el-col>
              </el-row>
              <div class="title">基本信息</div>
              <el-row>
                <el-col>
                  <el-form-item label-width="100px"  label="审计事项:" prop="auditMatter">
                    <el-input v-model="form.auditMatter" :autosize="{ minRows: 2}" type="textarea"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col>
                  <el-form-item label-width="100px"  label="审计过程:" prop="auditProcess">
                    <el-input v-model="form.auditProcess" :autosize="{ minRows: 2}" type="textarea"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col>
                  <el-form-item label-width="100px"  label="审计认定的事实摘要及审计结论:" prop="auditConclusion">
                    <el-input v-model="form.auditConclusion" :autosize="{ minRows: 4}" type="textarea"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <div class="title">疑点信息</div>
              <el-row>
                <el-col>
                  <el-table
                    ref="table"
                    :data="tableData"
                    :model="tableData"
                    style="width: 80%;margin-left: auto; margin-right: auto;"
                    max-height="450"
                    highlight-current-row
                  >
<!--                    <el-table-column type="selection" width="55" align="center"></el-table-column>-->
                    <el-table-column label="序号" width="100">
                      <template #default="{ $index }">
                        <span>{{ $index + 1 }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column label="疑点编号" width="200">
                      <template #default="{ row }">
                        {{row.pointCode}}
                      </template>
                    </el-table-column>
                    <el-table-column label="是否落实" width="100">
                      <template #default="{ row }">
                        {{row.fix | yesOrNo}}
                      </template>
                    </el-table-column>
                    <el-table-column label="是否发现" width="100">
                      <template #default="{ row }">
                        {{row.discovery | yesOrNo}}
                      </template>
                    </el-table-column>
                    <el-table-column label="疑点描述">
                      <template #default="{ row }">
                        {{row.pointDesc}}
                      </template>
                    </el-table-column>
                  </el-table>
                </el-col>
              </el-row>
              <div class="title">工作底稿文书</div>
              <el-row >
                <el-col :span="24">
                  <el-form-item label-width="100px" label="工作底稿:" prop="projectName">
                    <web-document :form-data="form" readWord="edit" @createWordFile="wordFileHandle" :pageType="'worksheet'"/>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                    <el-button icon="el-icon-close" size="small" @click="cancelDialog">关闭</el-button>
                    <el-button icon="el-icon-check" size="small" type="primary" @click="submit">保存</el-button>
            </span>
          </div>
        </div>
      </div>
    </div>
    <formTransfer :multiple="multiple" :transferDialog="transferDialog" :transferDialogType="transferDialogType"
                  @closeTransfer="closeTransfer" @transferData="transferDataHandle"/>
  </el-dialog>
</template>

<script>
import FormTransfer from "@/components/form/transfer";
import {getObj, submit} from "@/api/adis/execute/paper";
import WebDocument from '@/components/form/webDocument.vue';
import {yesOrNo} from "@/filters";

export default {
  components: {
    FormTransfer,
    WebDocument
  },
  data() {
    return {
      visible: false,
      canSubmit: false,
      multiple: false,
      transferDialog: false,
      transferDialogType: '',
      transferDialogTypeArr: [],
      form: {},
      tableData: [ // 表格数据
        {
          templateId: '',
        }
      ],
      selectedRows: [], // 存储选中的行数据
      nextId: 2, // 序号计数器
      formDisplay:false,
    }
  },
  watch: {
    tableData(newVal) {
      if (newVal.length < 1) {
        this.onAddItem(this.tableData)
      }
    },
  },
  methods: {
    yesOrNo,
    init(id) {
      this.canSubmit = true;
      this.$nextTick(async () => {
        if (id) {
          const response = await getObj(id);
          this.form = {...response.data.data};
          this.tableData = this.form.projectPointList;
        }
        this.visible = true;
      })
    },
    updateAuditTypeSubOptions(auditType) {
      // 根据选择的审计类型更新审计子类型的选项
      this.form.auditTypeSubList = this.auditTypeSubList[auditType];
    },
    addSelectedRow() {
      const selectedRow = this.selectedRows;
      const index = this.tableData.indexOf(selectedRow);
      if (index !== -1) {
        // 复制当前行数据添加到表格数据中
        this.onAddItem(selectedRow)
      }
    },
    clearTableData() {
      this.tableData = [];
    },
    onAddItem(val) {
      // 在这里添加添加新项目的逻辑
      this.tableData.push({
        templateId: val.templateId,
        code: val.code,
        evidenceType: val.evidenceType,
        evidenceDate: val.evidenceDate,
        evidenceName: val.evidenceName,
      })
    },
    deleteSelectedRows() {
      // 遍历选中的行数据，并从数据源中删除
      const selectedRow = this.selectedRows;
      const index = this.tableData.indexOf(selectedRow);
      if (index !== -1) {
        this.tableData.splice(index, 1);
      }
    },
    selectableFunc(row, index) {
      // 这里返回 true 或 false 来决定是否可以选中这一行。
      // 一般来说，所有的行都应该可以被选中，所以可以返回 true。
      return true;
    },
    updateSelectedRows(selection) {
      this.selectedRows = selection;
    },
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    handlePreview(file) {
      window.open(file.url ? file.url : file.response.data.url)
    },
    // 表单提交
    submit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.fileList && this.fileList.map((file) => {
            this.form.fileUrl = file.response ? file.response.data.url : file.url
          });
          this.loading = true;
          submit(this.form).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.cancelDialog();
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    //重置表单
    cancelDialog() {
      this.visible = false;
      this.form = {};
      this.$emit('cancelDialog', {})
      this.$refs['dataForm'].resetFields()
    },
    showUserDialog(arr, type, multiple) {
      this.transferDialogTypeArr = arr
      this.transferDialogType = type
      this.multiple = multiple
      this.transferDialog = true
    },
    closeTransfer() {
      this.transferDialog = false
      this.transferDialogType = ''
    },
    // 已选定数据的更新判断
    transferDataHandle(e) {
      this.transferDialog = false
      let currentval = [], currentLabel = []
      if (e.list.length > 0) {
        if (e.list.length > 1) {
          e.list.forEach(item => {
            currentval.push(item.id)
            currentLabel.push(item.name)
          })
        } else {
          currentval = [e.list[0].id]
          currentLabel = [e.list[0].name]
        }
        this.$set(this.form, this.transferDialogTypeArr[0], currentLabel.join(','))
        this.$set(this.form, this.transferDialogTypeArr[1], currentval.join(','))
      } else { // 如果返回的数据为空，也将当前页面数值清空
        this.$set(this.form, this.transferDialogTypeArr[0], '')
        this.$set(this.form, this.transferDialogTypeArr[1], '')
      }
      this.transferDialogType = ''
    },
    wordFileHandle(e) {
      this.$set(this.form, "wordFile", e.wordFile);
      this.$set(this.form, "paperId", e.id);
    }
  }
}
</script>
